package com.lsz.intelligentflowerwatering.ui.theme

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.TweenSpec
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.material.MaterialTheme
import androidx.compose.material.MaterialTheme.shapes
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color


private val LightColorPalette = MyComposeColors(
    buttonBackground = buttonBackground,
    buttonContent = buttonContent,
    listBackground = listBackground,
    listContent = listContent,
    statusBar = statusBar,
    toolbarBackground = toolbarBackground,
    toolbarContent = toolbarContent,
    background = background,
    dialog = dialog,
    alpha = 1f
)

private val DarkColorPalette = MyComposeColors(
    buttonBackground = buttonBackground,
    buttonContent = buttonContent,
    listBackground = listBackground,
    listContent = listContent,
    statusBar = statusBar,
    toolbarBackground = toolbarBackground,
    toolbarContent = toolbarContent,
    background = background,
    dialog = dialog,
    alpha = 1f
)

@Stable
class MyComposeColors(
    buttonBackground: Color,
    buttonContent: Color,
    listBackground: Color,
    listContent: Color,
    statusBar: Color,
    toolbarBackground: Color,
    toolbarContent: Color,
    background: Color,
    dialog : Color,
    alpha: Float
) {
    val buttonBackground by mutableStateOf(buttonBackground)
    val buttonContent by mutableStateOf(buttonContent)
    val listBackground by mutableStateOf(listBackground)
    val listContent by mutableStateOf(listContent)
    val statusBar by mutableStateOf(statusBar)
    val toolbarBackground by mutableStateOf(toolbarBackground)
    val toolbarContent by mutableStateOf(toolbarContent)
    val background by mutableStateOf(background)
    val dialog by mutableStateOf(dialog)
    val alpha by mutableStateOf(alpha)
}

private val LocalMyComposeColors = compositionLocalOf {
    LightColorPalette
}

object MyComposeTheme {
    val colors: MyComposeColors
        @Composable
        get() = LocalMyComposeColors.current

    enum class Theme {
        Light, Dark
    }
}

@Composable
fun MyComposeTheme(
    theme: MyComposeTheme.Theme = MyComposeTheme.Theme.Light,
    content: @Composable() () -> Unit
) {

    val targetColors = when (theme) {
        MyComposeTheme.Theme.Light -> LightColorPalette
        MyComposeTheme.Theme.Dark -> DarkColorPalette
    }

    val buttonBackground = animateColorAsState(targetColors.buttonBackground, TweenSpec(600))
    val buttonContent = animateColorAsState(targetColors.buttonContent, TweenSpec(600))
    val listBackground = animateColorAsState(targetColors.listBackground, TweenSpec(600))
    val listContent = animateColorAsState(targetColors.listContent, TweenSpec(600))
    val statusBar = animateColorAsState(targetColors.statusBar, TweenSpec(600))
    val toolbarBackground = animateColorAsState(targetColors.toolbarBackground, TweenSpec(600))
    val toolbarContent = animateColorAsState(targetColors.toolbarContent, TweenSpec(600))
    val background = animateColorAsState(targetColors.background, TweenSpec(600))
    val dialog = animateColorAsState(targetColors.dialog,TweenSpec(600) )
    val alpha = animateFloatAsState(targetColors.alpha, TweenSpec(600))

    val colors = MyComposeColors(
        buttonBackground = buttonBackground.value,
        buttonContent = buttonContent.value,
        listBackground = listBackground.value,
        listContent = listContent.value,
        statusBar = statusBar.value,
        toolbarBackground = toolbarBackground.value,
        toolbarContent = toolbarContent.value,
        background = background.value,
        dialog = dialog.value,
        alpha = alpha.value
    )

    CompositionLocalProvider(LocalMyComposeColors provides colors) {
        MaterialTheme(
            shapes = shapes,
            content = content
        )
    }
}